package org.kkhweb.checkbook;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class SyncActivity extends Activity {

	private SimpleHTTPClient httpClient;
	private SimpleFiles files;
	private	ProgressDialog dialog;

	private SharedPreferences settings;
	private Editor editor;
    public static final String PREFS_NAME = "MyCheckbookSettings";	
	public static final String CHECKBOOKS_FILE = "Checkbooks_JSON_File";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    this.restore_settings();
    	syncThread();

	}//End onCreate

	@Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
    	this.save_settings();
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    } 
    
//Sync Thread    
	protected void syncThread()
	{
		httpClient = new SimpleHTTPClient();
		
    	dialog = ProgressDialog.show(SyncActivity.this, "Syncing With Server", 
                "Please wait...", false);  		    

	    new Thread() {

			@Override
		    public void run()
		    {         
		    	httpClient.addFormData("user_id", settings.getString("user_id", ""));
		    	httpClient.setURL(Globals.buildURL("get_books"));
		    	httpClient.sendRequest();
		        
		        handler.sendEmptyMessage(0);
		    }
	    }.start();
	}        

//Handler for SyncThread    
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			dialog.dismiss();
    		
    		if(httpClient.hasError()) {
    			toast(httpClient.getError());
    		} else {
    			files = new SimpleFiles(getApplicationContext());
    			files.saveFile(CHECKBOOKS_FILE, httpClient.getResponse());
    			if(files.hasError()) {
    				toast(files.getError());
    			} else {
    				toast("Checkbook Sync Complete");
    			}
        		finish();	
    		}
		}
	};	
	
	//Toast Messages    
    private void toast(String msg)
    {
    	Context context = getApplicationContext();
    	Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();    	
    }  	
	
	private void restore_settings()
	{
        settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = settings.edit();        
	}
	
	private void save_settings()
	{
		editor.commit();		
	}		
	
}//End SyncActivity